/**
 * 登录失效的处理逻辑
 */
import urlHandler from 'utils/helper/url-handler'
import storage from 'utils/helper/storage'
import sdk from 'sdk'

// 白名单列表的接口返回401无需重新登录
const whiteListApi = ['member/load', 'member/getPrimeMebBalance', 'member/getMebBalance']

export default function (xhr) {
  let { request, config } = xhr
  // 未登录状态可能是token失效，需要删除token
  storage.remove('AUTHORIZATION')
  let path = urlHandler.parseURL(request.responseURL).path
  path = path.replace(config.baseURL, '').replace(/^\//, '')

  if (whiteListApi.indexOf(path) >= 0) {
    return
  }
  sdk.login().then((res) => {
    let token = res && res.token
    if (token) {
      window.location.reload()
    }
  })
}
